using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using MaterialSkin;
using MaterialSkin.Controls;
using System.Diagnostics;

namespace VideoAutoGenerator.UI.Forms
{
    public partial class MainForm : MaterialForm
    {
        private readonly MaterialSkinManager materialSkinManager;
        private MaterialTabControl mainTabControl;
        private MaterialButton configButton;
        private MaterialButton worksButton;
        private MaterialButton generateButton;
        private MaterialButton batchButton;
        private MaterialButton logButton;

        private void StylePanels()
        {
            // Style navigation panel
            this.navigationPanel.BackColor = Color.FromArgb(55, 71, 79);
            this.navigationPanel.ForeColor = Color.White;
            
            // Style status bar panel
            this.statusBarPanel.BackColor = Color.FromArgb(33, 150, 243);
            this.statusBarPanel.ForeColor = Color.White;
            
            // Add some padding to navigation panel
            this.navigationPanel.Padding = new Padding(10);
        }

        public MainForm()
        {
            InitializeComponent();

            // 初始化MaterialSkinManager
            materialSkinManager = MaterialSkinManager.Instance;
            materialSkinManager.AddFormToManage(this);
            materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
            materialSkinManager.ColorScheme = new ColorScheme(
                Primary.BlueGrey800, 
                Primary.BlueGrey900, 
                Primary.BlueGrey500, 
                Accent.LightBlue200, 
                TextShade.WHITE
            );
            
            // Setup navigation buttons
            SetupNavigation();
            
            // Setup main tab control
            SetupMainTabControl();
            
            // Style the panels to match Material design
            StylePanels();
        }
        
        private void SetupNavigation()
        {
            // Create navigation buttons
            configButton = new MaterialButton();
            worksButton = new MaterialButton();
            generateButton = new MaterialButton();
            batchButton = new MaterialButton();
            logButton = new MaterialButton();
            
            // Configure navigation buttons
            ConfigureNavButton(configButton, "配置管理", 0);
            ConfigureNavButton(worksButton, "作品管理", 1);
            ConfigureNavButton(generateButton, "视频生成", 2);
            ConfigureNavButton(batchButton, "批量处理", 3);
            ConfigureNavButton(logButton, "系统日志", 4);
            
            // Add event handlers
            configButton.Click += (sender, e) => ShowTabPage(0);
            worksButton.Click += (sender, e) => ShowTabPage(1);
            generateButton.Click += (sender, e) => ShowTabPage(2);
            batchButton.Click += (sender, e) => ShowTabPage(3);
            logButton.Click += (sender, e) => ShowTabPage(4);
            
            // Add buttons to navigation panel in reverse order because of DockStyle.Top
            this.navigationPanel.Controls.Add(logButton);
            this.navigationPanel.Controls.Add(batchButton);
            this.navigationPanel.Controls.Add(generateButton);
            this.navigationPanel.Controls.Add(worksButton);
            this.navigationPanel.Controls.Add(configButton);
        }
        
        private void ConfigureNavButton(MaterialButton button, string text, int index)
        {
            button.Margin = new Padding(0);
            button.Dock = DockStyle.Top;
            button.Text = text;
            button.Height = 40;
            button.TextAlign = ContentAlignment.MiddleLeft;
            button.FlatStyle = FlatStyle.Flat;
            button.ForeColor = Color.White;
            button.BackColor = Color.FromArgb(75, 91, 99);
            button.Tag = index; // Store index for identification
        }
        
        private void ShowTabPage(int index)
        {
            if (mainTabControl.TabCount > index)
            {
                mainTabControl.SelectedIndex = index;
                string tabName = mainTabControl.TabPages[index].Text;
                UpdateStatusBar($"就绪 - {tabName}");
                
                // Force the tab control to refresh
                mainTabControl.Refresh();
            }
        }

        private void SetupMainTabControl()
        {
            mainTabControl = new MaterialTabControl();
            mainTabControl.Dock = DockStyle.Fill;
            
            // Create tab pages
            TabPage configTabPage = new TabPage("配置管理");
            TabPage worksTabPage = new TabPage("作品管理");
            TabPage generateTabPage = new TabPage("视频生成");
            TabPage batchTabPage = new TabPage("批量处理");
            TabPage logTabPage = new TabPage("系统日志");
            
            // Add tab pages to tab control
            mainTabControl.TabPages.Add(configTabPage);
            mainTabControl.TabPages.Add(worksTabPage);
            mainTabControl.TabPages.Add(generateTabPage);
            mainTabControl.TabPages.Add(batchTabPage);
            mainTabControl.TabPages.Add(logTabPage);
            
            // Setup content for each tab
            SetupTabContent(configTabPage, "配置管理界面");
            SetupTabContent(worksTabPage, "作品管理界面");
            SetupTabContent(generateTabPage, "视频生成界面");
            SetupTabContent(batchTabPage, "批量处理界面");
            SetupTabContent(logTabPage, "系统日志界面");
            
            // Add tab control to content panel
            this.contentPanel.Controls.Add(mainTabControl);
            
            // Subscribe to tab selection event
            mainTabControl.Selecting += (sender, e) =>
            {
                string tabName = mainTabControl.TabPages[e.TabPageIndex].Text;
                UpdateStatusBar($"就绪 - {tabName}");
            };
            
            // Set initial status
            UpdateStatusBar("就绪 - 配置管理");
        }
        
        private void SetupTabContent(TabPage tabPage, string contentText)
        {
            // Check if this is the configuration tab
            if (tabPage.Text == "配置管理")
            {
                // Create and show configuration form
                var configForm = new ConfigForms.ConfigurationForm();
                configForm.TopLevel = false;
                configForm.FormBorderStyle = FormBorderStyle.None;
                configForm.Dock = DockStyle.Fill;
                tabPage.Controls.Add(configForm);
                configForm.Show();
            }
            // Check if this is the video generation tab
            else if (tabPage.Text == "视频生成")
            {
                // Create and show video generation control
                var videoGenerationControl = new UserControls.VideoGenerationControl();
                videoGenerationControl.Dock = DockStyle.Fill;
                tabPage.Controls.Add(videoGenerationControl);
            }
            // Check if this is the batch processing tab
            else if (tabPage.Text == "批量处理")
            {
                // Create and show batch processing control
                var batchProcessingControl = new UserControls.BatchProcessingControl();
                batchProcessingControl.Dock = DockStyle.Fill;
                tabPage.Controls.Add(batchProcessingControl);
            }
            // Check if this is the work management tab
            else if (tabPage.Text == "作品管理")
            {
                // Create and show work management control
                var workManagementControl = new UserControls.WorkManagementControl();
                workManagementControl.Dock = DockStyle.Fill;
                tabPage.Controls.Add(workManagementControl);
            }
            else
            {
                // For other tabs, just show a label with the tab name
                MaterialLabel label = new MaterialLabel();
                label.Text = contentText;
                label.Dock = DockStyle.Fill;
                label.TextAlign = ContentAlignment.MiddleCenter;
                
                tabPage.Controls.Add(label);
            }
        }

        private void UpdateStatusBar(string message)
        {
            this.statusBarPanel.Controls.Clear();
            MaterialLabel label = new MaterialLabel();
            label.Text = message;
            label.Dock = DockStyle.Fill;
            label.TextAlign = ContentAlignment.MiddleLeft;
            label.ForeColor = Color.White;
            this.statusBarPanel.Controls.Add(label);
        }
    }
}